 clear all
set mem 500m
set more off
************************************************************************************
clear
use data_working

gen alos=(daysnp+daysfp+dayspb)/admtot
summ alos if admtot>1
** generate per capita beds **
gen popul=ipop/1000
gen bedstot_temps=beds_tot
replace bedstot_temps=0 if beds_tot==1
gen bedsnppub_temps=beds_nppub
replace bedsnppub_temps=0 if beds_nppub==1
gen bedsfp_temps=bdtotfp
replace bedsfp_temps=0 if bdtotfp==1

replace popul=0 if popul==.
gen bdpc=bedstot_temps/(popul+1)
gen bdpc_nppub=bedsnppub_temps/(popul+1)
gen bdpc_fp=bedsfp_temps/(popul+1)

gen admtotpc=admtot/(popul+1)
gen admnppubpc=adm_nppub/(popul+1)
gen admfppc=admtotfp/(popul+1)

** create first_treat event **
drop if year==1947
sort fcounty year
bys fcount : gen first_treat=sum(treat_it)

** gen year first treated **
forvalues i=1/1{
	gen yeart`i'=year if treat_it==1 & first_treat==`i'
	by fcounty1: egen yeart`i'2=mean(yeart`i')
	drop yeart`i'
	rename yeart`i'2 yeart`i'
	}

replace first_treat=1 if first_treat>0

** gen treat^k's **
forvalues i=1/1{
	gen chyears`i'=year-yeart`i'
	}




gen chyrpos1=chyears1 if chyears1>=0
replace chyrpos1=-1 if chyears1<0
gen placebo1=chyears1 if chyears1>=-9
replace placebo1=-10 if chyears1<-9 & yeart!=.
replace placebo1=21 if chyears1>=21 
replace placebo1=. if yeart==.

by fcounty: gen test1=sum(treat_it)
sort fcounty test1
by fcounty test1: gen test2=_n

xi i.chyrpos1 i.placebo1 i.year 

forvalues i=2/32 {
	replace _Iplacebo1_`i'=0 if _Iplacebo1_`i'==.
  }

** Summary stats**
summ yeart if yeart!=., det
summ beds* if year==1953 & yeart==., det
summ admtot adm_npp admtotfp if year==1953 & yeart==., det
gen firmstot=firmsfp+firmsnp+firmspb
gen firms_nppub=firmsnp+firmspb
summ firmstot firms_npp firmsf if year==1953 & yeart==., det
replace daysfp=0 if missing(daysfp)
replace daysnp=0 if missing(daysnp)
replace dayspb=0 if missing(dayspb)

gen daystot=daysfp+daysnp+dayspb
gen days_nppub=daysnp+daysp
summ daystot days_npp daysf if year==1953 & yeart==., det
summ ipop imedfaminc ipop65 ipoplt5 inwpop_pct infmd if year==1953 & yeart==.,det
summ beds* if year==1953 ,det
summ admtot adm_npp admtotfp if year==1953,det
summ firmstot firms_npp firmsf if year==1953 ,det
summ daystot days_npp daysf if year==1953 ,det
summ ipop imedfaminc ipop65 ipoplt5 inwpop_pct infmd if year==1953 ,det
summ beds* if _Iplacebo1_11==1,det
summ admtot adm_npp admtotfp if _Iplacebo1_11==1,det
summ firmstot firms_npp firmsf if _Iplacebo1_11==1,det
summ daystot days_npp daysf if _Iplacebo1_11==1,det
summ ipop imedfaminc ipop65 ipoplt5 inwpop_pct infmd if _Iplacebo1_11==1,det

label var _Ichyrpos1_2 "Year of Change"
label var _Ichyrpos1_3 "1 Year Later"
forvalues i=4/11 {
	local j = `i' - 2 
	label var _Ichyrpos1_`i' "`j' Years Later"
}
label var _Ichyrpos1_12 "10+ Years Later"


label var _Iplacebo1_2 "9 Years Prior"
label var _Iplacebo1_3 "8 Year Prior"
label var _Iplacebo1_4 "7 Years Prior"
label var _Iplacebo1_5 "6 Years Prior"
label var _Iplacebo1_6 "5 Years Prior"
label var _Iplacebo1_7 "4 Years Prior"
label var _Iplacebo1_8 "3 Years Prior"
label var _Iplacebo1_9 "2 Years Prior"
label var _Iplacebo1_10 "1 Years Prior"
label var _Iplacebo1_11 "Year of HB Funding"
forvalues i=12/31 {
	local j = `i' - 11
	label var _Iplacebo1_`i' "`j' Years Later"
}
label var _Iplacebo1_32 "21+ Years Later"

gen ipop2=ipop/100000
replace yeart=3000 if yeart==.

forvalues i=2/32 {
	replace _Iplacebo1_`i'=0 if _Iplacebo1_`i'==.
  }

forvalues i=11/32 {
	local j = `i' - 11
	rename _Iplacebo1_`i' treat_lag`j'
}
forvalues i=2/10 {
	local j = 11 - `i'
	rename _Iplacebo1_`i' treat_lead`j'
}

gen pop65_pct=ipop65/ipop
gen poplt5_pct=ipoplt5/ipop

sort fcounty1 year

gen diff2bd=abs(d2.beds_tot)
egen maxdiff2=max(diff2bd), by(fcounty)


bys fcounty : gen dropvar=1 if maxdiff2>200 & beds_tot==1 & beds_tot[_n-1]>1 & beds_tot[_n+1]>1  & year>=1948
replace dropvar=0 if missing(dropvar)
tab fcounty if dropvar==1

keep if dropvar==0 
* create MDs per capita *
gen infmdpc=infmd/(popul+1)

*******************************************************
*******************************************************
gen hbfundpc_totinfladjust=hbfund_totinfladjust/ipop
by fcounty1: egen sumhbfundpc=sum(hbfundpc_totinfladjust)

* mean and std dev of overall fund for interpretation of coeffs*
sum sumhbfundpc if year==1975
sum sumhbfundpc if year==1975 & sumhbfundpc >0


* create state variables for state FEs *
egen statecd=group(stab)
replace statecd=30 if fcounty1>36000 & fcounty1<37000
replace statecd=29 if fcounty1>35000 & fcounty1<36000
replace statecd=26 if fcounty1>32000 & fcounty1<33000
replace statecd=23 if fcounty1>29000 & fcounty1<30000
replace statecd=18 if fcounty1>24000 & fcounty1<25000
replace statecd=8 if fcounty1>12000 & fcounty1<13000
replace statecd=48 if fcounty1>11000 & fcounty1<12000

xi i.statecd
replace sumhbfundp=sumhbfundpc/100

****************************
* Coefficients for Table 6 *
****************************

reg bdpc sumhbfundpc ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct _Istatecd* [aw=ipop2] if year==1975 
	outreg2 using longterm_nowt, replace noparen label tex se sym(***,**,*) title("Long Run Effect, 1975, 1980 and 1990")
reg bdpc sumhbfundpc ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct _Istatecd* [aw=ipop2] if year==1990 
	outreg2 using longterm_nowt, append noparen label tex se sym(***,**,*) title("Long Run Effect, 1975, 1980 and 1990")

reg bdpc_nppub sumhbfundpc ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct _Istatecd* if [aw=ipop2] year==1975 
	outreg2 using longterm_nowt, append noparen label tex se sym(***,**,*) title("Long Run Effect, 1975, 1980 and 1990")
reg bdpc_nppub sumhbfundpc ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct _Istatecd* if [aw=ipop2]  year==1990 
	outreg2 using longterm_nowt, append noparen label tex se sym(***,**,*) title("Long Run Effect, 1975, 1980 and 1990")

reg bdpc_fp sumhbfundpc ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct _Istatecd* [aw=ipop2] if year==1975 
	outreg2 using longterm_nowt, append noparen label tex se sym(***,**,*) title("Long Run Effect, 1975, 1980 and 1990")
reg bdpc_fp sumhbfundpc ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct _Istatecd* [aw=ipop2] if year==1990 
	outreg2 using longterm_nowt, append noparen label tex se sym(***,**,*) title("Long Run Effect, 1975, 1980 and 1990")

